//
//  NSObject+Protect.m
//  CategarySwizzle
//
//  Created by wangrui on 14-1-8.
//  Copyright (c) 2014年 PPS. All rights reserved.
//

#import "NSArray+Protect.h"
#import <objc/runtime.h>
#import <objc/objc.h>
#import <objc/message.h>

@implementation NSArray (Protect)

/**
 * hook不了,NSArray的方法
 */
+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self hookNSObjectMethod];
    });
}

+ (void)hookNSObjectMethod{
    Method originMethod = class_getInstanceMethod([NSMutableArray class], @selector(addObject:));
    
    IMP addIMP =  method_getImplementation(originMethod);
    SEL sel = sel_registerName("copyAddObject:");
    class_addMethod([NSObject class],sel, addIMP, method_getTypeEncoding(originMethod));
    
    Method replaceMethod = class_getInstanceMethod([self class], @selector(hookNSObjectMethodSelector:));
    method_setImplementation(originMethod, method_getImplementation(replaceMethod));
}

- (void)hookNSObjectMethodSelector:(id)obj{
    NSLog(@"nsobject hook");
}

/**
 *  1,Other frameworks may rely on the expected behavior of the original method. We now have to go through the trouble of re-implementing that 
 *    behavior, in addition to the new functionality we wanted to introduce, or risk strange side effects and possibly even crashing out.
 *  2,If multiple categories implement the same method, the last one loaded wins! The load order is consistent within an application, but it’s 
 *   arbitrary, out of our hands, and fragile. For all we know, our implementation could itself be overwritten by an internal framework category!
 *
 *  由于各个类别load的顺序不同所以有可能,类别的方法名和原来的类一样时，不一定会替换原来的行为
 *
 */


@end
